// ==UserScript==
// @name         UAchange
// @description  简单的切换UA按钮
// @namespace    1018148046
// @author       颜太吓
// @include      chrome://browser/content/browser.xul
// @version      0.1
// @charset      UTF-8
// ==/UserScript==
'use strict';
(function() {
	let src = 'chrome://userchromejs/content/icons/other/UA.svg#desktop';
	let newsrc = 'chrome://userchromejs/content/icons/other/UA.svg#Phone';
	let uacPanel = document.createElement('image');
	uacPanel.setAttribute('id', 'uacPanel');
	uacPanel.setAttribute('class', 'urlbar-icon');
	uacPanel.setAttribute('tooltiptext', '切换UA');
	let preferencesService = Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefService).getBranch('');
	if (preferencesService.getPrefType('general.useragent.override') == 0) {
		uacPanel.setAttribute('src', src);
	} else {
		uacPanel.setAttribute('src', newsrc);
	};
	document.getElementById('urlbar-icons').appendChild(uacPanel);
	uacPanel.addEventListener('click', function(event) {
		if (event.button == 0) {
			if (preferencesService.getPrefType('general.useragent.override') == 0) {
				preferencesService.setCharPref('general.useragent.override', 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30');
				uacPanel.setAttribute('src', newsrc);
			} else {
				preferencesService.clearUserPref('general.useragent.override');
				uacPanel.setAttribute('src', src);
			}
		};
	}, false);
})();